package com.attilax.util;

import java.io.File;
import java.text.Collator;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

//import org.jfree.chart.ChartFactory;
















import com.attilax.Closure;
import com.attilax.clr.garLib;
import com.attilax.secury.MD5_BigFile;

import m.dirPkg.travDir;


/**
 * move "e:\ati\doc\2012.10.jenleiD\2012.10.jenleiD" e:\0
move "e:\ati\doc\2012.10.jenleiE\2012.10.jenleiE" e:\0
move "e:\ati\doc\2012.10.jenleiF\2012.10.jenleiF" e:\0
move "e:\ati\doc\2012.10.jenleiG\2012.10.jenleiG" e:\0
move "e:\ati\doc\2012.10.jenleiH\2012.10.jenleiH" e:\0
move "e:\ati\doc\2012.10.jenleiHA\2012.10.jenleiHA" e:\0
move "e:\ati\doc\2012.10.jenleiHedsi\2012.10.jenleiHedsi" e:\0
move "e:\ati\doc\2012.10.jenleiUZ\2012.10.jenleiUZ" e:\0
move "e:\ati\doc\2012.10jenleiA\2012.10jenleiA" e:\0
move "e:\ati\doc\2012.10jenleiB\2012.10jenleiB" e:\0
move "e:\ati\doc\2012.12\2012.12" e:\0
move "e:\ati\doc\20120315\20120315" e:\0
move "e:\ati\doc\201205\201205" e:\0
move "e:\ati\doc\2013.01\2013.01" e:\0
move "e:\ati\doc\2013.02\2013.02" e:\0
move "e:\ati\doc\2013.03\2013.03" e:\0
move "e:\ati\doc\2013.04\2013.04" e:\0
move "e:\ati\doc\2013.05\2013.05" e:\0
move "e:\ati\doc\2013.06\2013.06" e:\0
move "e:\ati\doc\bakc0125\bakc0125" e:\0
move "e:\ati\doc\bakc227\bakc227" e:\0
move "e:\ati\doc\o5\o5" e:\0

 * @author Administrator
 *
 */
public class dirx {
	public static int i=0;
	 

	public static void main(String[] args) {
	//	System.out.println(	moveFolder("E:\\ati\\doc\\2012.10.jenleiD\\2012.10.jenleiD",  "E:\\ati\\doc"));
	//	System.out.println("-f");
		
		dirx.trave_all("e:\\ati\\doc", new Closure () {

			@Override
			public Object execute(Object arg0) throws Exception {
				 String p=(String) arg0;
				  File dir = new File(p);
				
				  if(dir.isDirectory())
				  {
					 
					  String dirName=dir.getName();
					//  System.out.println(dirName);
					 // if(dirName.contains("2012.10.jenleiD"))
					//	  System.out.println("");
					  String parentDirName=  dir.getParentFile().getName();
					if(dirName.equals(parentDirName))
						if(dir.getParentFile().getParentFile().getName().equals("doc"))
					  
					  System.out.println("move \""+dir +"\" e:\\0");
				  }
				//  System.out.println(i++);
				return null;
			}
		});
	}
	
	 static boolean moveFolder(String src, String dest) {
		  File srcFolder = new File(src);
		  File destFolder = new File(dest);
		  String pathname = destFolder.getAbsoluteFile() + "\\" + srcFolder.getName();
		File newFile = new File(pathname);
return 	  srcFolder.renameTo(newFile);
		 }

	public static List<String> getFiles(String strPath) {
		travDir.refreshFileList(strPath);
		List<String> li = travDir.filelist;
		
		return li;
	}

	/**
	@author attilax ���۵�צ��
		@since  2014-5-27 ����02:42:43$
	
	 * @param s
	 * @return
	 */
	public static String getParentPath(String s) {
		// attilax ���۵�צ��  ����02:42:43   2014-5-27 
		File f=new File(s); 
		return f.getParent();
	}
	/**
	 * 
	@author attilax 老哇的爪子
	\t@since  Aug 2, 2014 3:40:56 AM$
	trave safe   trave_files_noIncludeDir
	
	 * @param strPath
	 */
	@SuppressWarnings("all")
	public static void trave(String strPath ,Closure closure) { 
	    File dir = new File(strPath); 
	    if(!dir.exists())
	    	throw new RuntimeException("no this dir:"+strPath);
	    File[] files = dir.listFiles(); 
	    
	    if (files == null) 
	        return; 
	    int length = files.length;
		for (int i = 0; i < length; i++) { 
			
			try {
				 if (files[i].isDirectory()) { 
			        	trave(files[i].getAbsolutePath(),closure); 
			        } else { 
			            String strFileName = files[i].getAbsolutePath().toLowerCase();
			            
			            try {
							closure.execute(files[i].getAbsolutePath());
						} catch (Exception e) {
							e.printStackTrace();
						}  
			            
			        } 
			} catch (Exception e) {
				e.printStackTrace();
			}
	       
	    } 
	}
	
	
	public static void trave_all(String strPath ,Closure closure) { 
		
		   
        try {
			closure.execute(strPath);
		} catch (Exception e) {
			e.printStackTrace();
		}  
	    File dir = new File(strPath); 
	    if(!dir.exists())
	    	throw new RuntimeException("no this dir:"+strPath);
	    File[] files = dir.listFiles(); 
	    
	    if (files == null) 
	        return; 
	    int length = files.length;
		for (int i = 0; i < length; i++) { 
			try {
				 if (files[i].isDirectory()) { 
			        	trave_all(files[i].getAbsolutePath(),closure); 
			        } else { 
			            String strFileName = files[i].getAbsolutePath().toLowerCase();
			            
			            try {
							closure.execute(files[i].getAbsolutePath());
						} catch (Exception e) {
							e.printStackTrace();
						}  
			            
			        } 
			} catch (Exception e) {
				e.printStackTrace();
			}
	       
	    } 
	}
	
	/**  
	 *  not safe ver
	@author attilax 老哇的爪子
	\t@since  Aug 2, 2014 3:40:56 AM$
	
	 * @param strPath
	 */
	public static void trave_NS(String strPath ,Closure closure) { 
	    File dir = new File(strPath); 
	    File[] files = dir.listFiles(); 
	    
	    if (files == null) 
	        return; 
	    for (int i = 0; i < files.length; i++) { 
	        if (files[i].isDirectory()) { 
	        	trave(files[i].getAbsolutePath(),closure); 
	        } else { 
	            String strFileName = files[i].getAbsolutePath().toLowerCase();
	            
	            try {
					closure.execute(files[i].getAbsolutePath());
				} catch (Exception e) {
					throw  new RuntimeException("",e);
				}  
	            
	        } 
	    } 
	}

	public static void trave_files_noIncludeDir(String dir, Closure closure) {
		trave(dir,closure);
		
	}

	public static List traveASListOrderbyPath_wzDir(String dir) {
		 final List li=new ArrayList();
		 dirx.trave_all( dir, new Closure() {

				@Override
				public Object execute(Object arg0) throws Exception {
					String p = (String) arg0;
					
				 li.add(p);

					// System.out.println("move \""+dir +"\" e:\\0");

					// System.out.println(i++);
					return null;
				}
			});
		return sort( li);
	}
	
	public static List traveASListOrderby(String dir) {
		 final List li=new ArrayList();
		 dirx.trave_files_noIncludeDir(dir, new Closure() {

				@Override
				public Object execute(Object arg0) throws Exception {
					String p = (String) arg0;
					
				 li.add(p);

					// System.out.println("move \""+dir +"\" e:\\0");

					// System.out.println(i++);
					return null;
				}
			});
		return sort( li);
	}

	private static List sort(List li) {
		Collections.sort(li,Collator.getInstance(java.util.Locale.CHINA));
		return li;
	}

}
